package actions;
/**
 * @author Bailey Steinfadt
 * 
 * This class checks to see if there is the same number of braces
 */
import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;


public class Parentest extends AbstractAction{

	private static final long serialVersionUID = 1L;

	

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// Get the text pane and text
		JTextPane pane = (JTextPane)arg0.getSource();
		String text = pane.getText();
		//set variables for searching through the text for braces
		int sem = 0;
		int open = text.lastIndexOf('{'), close = text.lastIndexOf('}');
		//this will search backwards, so start at the end.
		int position = text.length()-1;

		//getting the style information so we can change text to red or black
		StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.red);
        AttributeSet unset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.black);
        StyledDocument doc = pane.getStyledDocument();
        
        //unhighlighting all braces..... for testing!
        if(position >= 0){
        	while(position >=0){
        		if((text.charAt(position) == '{') || (text.charAt(position) == '}')){
        			doc.setCharacterAttributes(position, 1, unset, false);
        		}
        		position--;
        	}
        	//and reset the position back to the end of the file
        	position = text.length()-1;
        }

        //if the position is less than zero, here, there's nothing in the file
        //so only search for braces if you have text
	    if(position >= 0){
			//check backwards for parenthesis 
			do{
				
				//if there's a closing brace, add one to the semaphore
				if(text.charAt(position) == '}') {
					sem++;
				}
				//if there's an opening brace, subtract one from the semaphore
				if(text.charAt(position) == '{') {
					open = position;	
					sem--;
				}
				
				//move back one position
				position--;

				//if the semaphore went under zero, there are too many open braces
				//highlight them red to show that they're probably not closed
				if(sem < 0){
					doc.setCharacterAttributes(open, 1, aset, false);
					//reset to zero because this one has been taken care of
					//and the file will be checked for more extras next iteration
					sem = 0;
				}//end if(sem < 0)
				
				//if the semaphore reaches zero, the braces after this point are okay
				//so now it will only check for errors before this point
				if(sem == 0){
					if(text.lastIndexOf('}') >= 0){
						close = text.lastIndexOf('}', open);
					}
				}
				
				//if position went below zero, there are too many closing braces
				//highlight them to show that they're probably extraneous
				if(position < 0){
					//you know how many extras you have based on the semaphore
					while(sem>0){
						doc.setCharacterAttributes(close, 1, aset, false);
						//have to look at close-1, or it will find the same brace repeatedly
						close = text.lastIndexOf('}', close-1);
						//count this one as taken care of
						sem--;
					}
				}//end if(position < 0)	
				
			}while((sem > 0) || (position >= 0));
			//exit loop when you've reached the front of the file, and you've taken care of extraneous closing braces
	    }	

	}

}
